<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="模板名称" prop="username">
        <el-input v-model.trim="form.mname" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addfee, editfee } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          mname: '',
          code: '',
          fee: '',
        },
        rules: {
          mname: [
            { required: true, trigger: 'blur', message: '请输入模板名称' },
          ],
          code: [
            { required: true, trigger: 'blur', message: '请输入品种代码' },
          ],
          fee: [{ required: true, trigger: 'blur', message: '请输入手续费额' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign(this.form, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },

      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.mid) {
              const res = await editfee(this.form)
              this.$baseMessage(res.msg, 'success')
            } else {
              console.log(this.form)
              const res = await addfee(this.form)
              this.$baseMessage(res.msg, 'success')
            }

            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
